package com.play.base_activity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;
import android.view.WindowManager;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;


import com.play.airfight.R;
import com.play.base_activity.base_view.TitleBar;
import com.socks.library.KLog;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

/**
 * Author by jmf,
 * Date on 2021/12/7.
 */
public abstract class BaseAbastractActivity extends RxAppCompatActivity {

    protected View mRootView;
    protected TitleBar mToolBar;

    public boolean isHasTitle() {
        return true;
    }

    public void onViewCreate() {
        int layoutId = getLayout();
        if (layoutId <= 0) {
            KLog.e("the activty layoutId  is illegal");
//            finish();
            return;
        }
        setContentView(R.layout.frame_base_main_layout);
        View contentView = createContentView(layoutId);
        if (null == contentView) {
            KLog.e("activity view inflater error ");
            finish();
            return;
        }

        mRootView = findViewById(R.id.frame_base_root);
        if (isToolBarEnable()) {
            TitleBar titleBar = findViewById(R.id.title_bar);
            titleBar.setVisibility(View.VISIBLE);
            mToolBar = titleBar;
            mToolBar.setIconOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });
        }
        initView(contentView);
    }

    private View createContentView(int layoutId) {
        ViewStub viewStub = findViewById(R.id.base_container);
        viewStub.setLayoutResource(layoutId);
        return viewStub.inflate();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //保持屏幕常亮
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        //系统栏风格
        onViewCreate();

    }

    /**
     *
     */
    protected abstract void initView(View contentView);


    /**
     * 加载 layout 文件
     *
     * @return
     */
    protected abstract
    @LayoutRes
    int getLayout();

    /**
     * 获取枚举类的标识
     *
     * @return ActivityType
     */
    protected abstract ActivtyType getActivityType();

    protected boolean isToolBarEnable() {
        return false;
    }


}
